<template>
<div style="max-width: 600px;" v-if="show">
  <x-form
      v-model="smodel"
      v-model:api="searchApi"
      :rules="searchRule"
      label-width="70"
  />
</div>
</template>
<script setup>

import tool from "@/utils/tool";
import bus from "@/store/bus";
import {ref, onMounted, onActivated,onDeactivated} from "vue";

const emit = defineEmits(['openBill'])
const listUrl = "bill/getInvoice"
const smodel = ref({})
const show = ref(false)
const fApi = ref({})
const searchApi = ref({})

const state = ref({
  showLog: false,
  loading: false,
  logList: [],
  billNo: '',
  salesId: '',
  salesName: '',
  currentRow: {},
  socket: null,
})
const searchRule = ref([])
const getRules = () => {
  let transType = fApi.value.getValue('transType')

  let ops = []
  if ([150601, 150602, 150603].indexOf(transType) !== -1) {
    ops.push({
      id: '业务员', label: '改业务员', click: (row) => {
        state.value.salesId = row.salesId
        state.value.salesName = row.salesName
        state.value.currentRow = row
        let options = {
          title: '修改票据业务员',
          width: 400,
          height: 200,
          showContinue: true,
          rules: [
            {field: 'salesName',title: '业务员', idField: 'salesId',com: 'JAuto', width: 260,cat: 'admin',defaultValue: row.salesName, defaultId: row.salesId, required: true},
          ],
          btns: [
            {
              name: '保存', click: (api, close) => {
                if(api.valid()) {
                  let formData = api.getFormData()
                  if (formData.salesId !== row.salesId) {
                    tool.post('bill/altBiz', {
                      billId: row.id,
                      salesId: formData.salesId,
                      salesName: formData.salesName
                    }).then(ret => {
                      if (ret.stat) {
                        state.value.currentRow.salesId = formData.salesId
                        state.value.currentRow.salesName = formData.salesName
                        openBill(row.billNo)
                        close()
                      }
                    })
                  }
                  else {
                    close()
                  }
                }
              }
            }
          ]
        }
        tool.editor(options)
      }
    })
  }
  ops.push({
    id: '日志', label: '日志', click: (row) => {
      tool.post('bill/getInvoiceLog', {billId: row.id}, true).then(ret => {
        if (ret.stat) {
          let list = ret.data
          let options = {
            title: '操作日志',
            width: 600,
            height: 500,
            rules: [
              {
                field: 'grid', title:'',com: 'JGrid', width: 530, height: 400,allFields: true,showFooter: false, columns: [
                  {type: 'index', width: 60,},
                  {field: 'ctime', title: '操作时间', width: 160,},
                  {field: 'name', title: '操作人', width: 150,},
                  {field: 'op', title: '操作内容', width: 100,},
                ],passList: list
              }
            ]
          }
          tool.editor(options)
        }
      })
    }
  })
  let fields = [
    {type:'index', width: 50,fixed: 'left',},
    {field: 'orgin', width: 60, title: '来源', align: 'center',fixed: 'left'},
    {field: 'billStateName', width: 60, title: '状态', align: 'center'},
    {field: 'billDate', width: 80, title: '日期', sortable: true, visible: true},
    {field: 'billNo', width: 125, title: '票号', sortable: true, visible: true},
    {field: 'buName', width: 160, title: '往来单位', sortable: true, align: 'left', visible: true},
    {
      field: 'totalAmount',
      width: 70,
      title: '总金额',
      align: 'right',
      sortable: true,
      formatter: 'formatMoney',
      visible: true
    },
    {
      field: 'op', width: 120, title: '操作',visible: true, align: 'center',fixed:'right', btns: ops
    },
  ]
  searchRule.value = [
    {field: 'transType', hide: true, defaultValue: transType},
    {field: 'buId', hide: true},
    {
      field: 'beginDate',
      title: '开始日期',
      com: 'JPicker',
      clearable: true,
      width: 220,
      defaultValue: tool.getDay('', -30)
    },
    {
      field: 'endDate',
      title: '结束日期',
      com: 'JPicker',
      width: 220,
      defaultValue: tool.getDay()
    },
    {
      field: 'buName',
      title: '客户',
      com: 'JAuto',
      cat: [150601, 150602, 150603, 150806, 153407, 153403, 153404, 153401, 153101].indexOf(transType) === -1 ? 'provider' : 'customer',
      clearable: true,
      width: 220,
      idField: 'buId'
    },
    {field: 'billNo', title: '票号', com: 'JInput', clearable: true, width: 220},
    {
      field: 'billState',title: '状态', com: 'JTab', list: [
        {name: '草稿', id: 'draft'},
        {name: '已审核', id: 'accounted'},
      ],  width: 220,defaultValue: 'draft',
      events: {
        change: () => {
          getList()
        }
      }
    },
    {
      field: '', com: 'JButton', text: '搜索', icon: 'search', width: 180, type:'info',
      marginLeft: 50,
      events: {
        click: () => {
          getList()
        }
      }
    },
    {
      field: 'grid', com: 'JGrid', columns: fields,offsetBottom: 10,listUrl: listUrl,
      inject: true,showPager: true,bindData: false,
      width: window.innerWidth - 1300,
      events: {
        cellClick: (api, row, column) => {
          if(column.title !== '操作') {
            openBill(row.billNo)
          }
        }
      }
    }
  ]
  show.value = true
}
const scanRet = ({code}) => {
  let arr = code.split('-')
  let transType = fApi.value.getValue('transType')
  if (tool.xe.toNumber(arr[0]) !== transType) {
    let transTypeName = fApi.value.getValue('transTypeName')
    return tool.err('不是 ' + transTypeName)
  }
  tool.post('bill/getBillNoById', {id: arr[1]}).then(ret => {
    if (ret.stat) {
      openBill(ret.data)
    }
  })
}
const changePage = () => {
  getList()
}
const refresh = () => {
  getList()
}
const getList = () => {
  searchApi.value.exec('grid', 'getList')
}
const openBill = (billNo) => {
  emit('openBill', billNo)
}

onMounted(() => {
  bus.$on('scan-ret', scanRet)
})
onActivated(() => {
  bus.$on('scan-ret', scanRet)
  getList()
})
onDeactivated(() => {
  bus.$off('scan-ret', scanRet)
  //global.socket.disconnect()
})
const setApi = (api) => {
  fApi.value = api
  getRules()
  setTimeout(() => {
    getList()
  }, 200)
}


defineExpose({getList, setApi, refresh})


</script>